<?php
require_once 'PHPUnit/Framework.php';

require_once 'TVB/ImageInfrastructure/Client/requires.php';

class ImageInstructureClientTest extends PHPUnit_Framework_TestCase
{
	static protected $source_id;
	static protected $export_id;
	
	public function testUpload() {
		$login_id = 'qoo';
		
		$ii_client = new ImageInfrastructureClient();
		$response = $ii_client->upload($login_id, 'file/test.jpg');
		$this->assertType(PHPUnit_Framework_Constraint_IsType::TYPE_OBJECT, $response);
		$this->assertGreaterThanOrEqual(1, $response->source_id);
		$this->assertRegExp('/http:\/\/ii\.tvb\.com\/.+/', $response->preview_url);
		
		ImageInstructureClientTest::$source_id = $response->source_id;
	}
	
	public function testGetSourceImageUrl() {
		$ii_client = new ImageInfrastructureClient();
		$url = $ii_client->getSourceImageUrl(ImageInstructureClientTest::$source_id);
		$this->assertRegExp('/http:\/\/ii\.tvb\.com\/.+/', $url);		
	}
	
	public function testRequestExport() {
		$login_id = 'qoo';
		$width = 50;
		$height = 50;
		$resize_mode = 2;
		$watermark = '';
		$format = '2';
		$export_arg = '85';
		$callback_url = 'http://ii.api.tvb.com/callback.php';

		$source_id = ImageInstructureClientTest::$source_id;
		
		$ii_client = new ImageInfrastructureClient();
		$export_id = $ii_client->requestExport($login_id, $source_id, $width, $height, $resize_mode, $watermark, $format, $export_arg, $callback_url);

		$this->assertGreaterThanOrEqual(1, $export_id);
		ImageInstructureClientTest::$export_id = $export_id;
	}
	
	public function testGetExportImageUrlEasily() {
		usleep(500000);
		$ii_client = new ImageInfrastructureClient();
		$url = $ii_client->getExportImageUrlEasily(ImageInstructureClientTest::$export_id);

		$this->assertRegExp('/http:\/\/ii\.tvb\.com\/.+/', $url);
	}
	
	public function testGetExportImageUrlTroublesomely() {
		$width = 50;
		$height = 50;
		$resize_mode = 2;
		$watermark = '';
		$format = '2';
		$export_arg = '85';
		
		$source_id = ImageInstructureClientTest::$source_id;
		
		$ii_client = new ImageInfrastructureClient();
		$url = $ii_client->getExportImageUrlTroublesomely($source_id, $width, $height, $resize_mode, $watermark, $format, $export_arg);
		$this->assertRegExp('/http:\/\/ii\.tvb\.com\/.+/', $url);
	}
	
	public function testRequestDistrubte() {
		$destination_id = 1;
		$login_id = 'qoo';
		$width = 50;
		$height = 50;
		$resize_mode = 2;
		$watermark = '';
		$format = '2';
		$export_arg = '85';
		$callback_url = 'http://ii.api.tvb.com/callback.php';

		$source_id = ImageInstructureClientTest::$source_id;
		
		$ii_client = new ImageInfrastructureClient();

		$export_id = $ii_client->requestDistribute($login_id, $source_id, $width, $height, $resize_mode, $watermark, $format, $export_arg, $destination_id, $callback_url);
		$this->assertGreaterThanOrEqual(1, $export_id);
		ImageInstructureClientTest::$export_id = $export_id;
	}

	public function testQueryStatusEasily() {
		sleep(5);
	
		$destination_id = 1;
		
		$source_id = ImageInstructureClientTest::$source_id;
		
		$ii_client = new ImageInfrastructureClient();
		$url = $ii_client->queryStatusEasily(ImageInstructureClientTest::$export_id, $destination_id);
		
		$this->assertRegExp('/http:\/\/img\.tvb\.com\/ii\/.+/', $url);
	}
	
	public function testQueryStatusTroublesomely() {
		$destination_id = 1;
		$width = 50;
		$height = 50;
		$resize_mode = 2;
		$watermark = '';
		$format = '2';
		$export_arg = '85';
		
		$source_id = ImageInstructureClientTest::$source_id;
		
		$ii_client = new ImageInfrastructureClient();

		$url = $ii_client->queryStatusTroublesomely($source_id, $width, $height, $resize_mode, $watermark, $destination_id, $format, $export_arg);
		$this->assertRegExp('/http:\/\/img\.tvb\.com\/ii\/.+/', $url);
	}
	
	public function testAddMeta() {
		$source_id = ImageInstructureClientTest::$source_id;
		$login_id = 'qoo';
		
		$title = 'aaa' . time();
		$person = 'bbb';
		$tag = 'ccc,ddd';
		$miscellaneous = array('tag1' => 'value1');
		
		$ii_client = new ImageInfrastructureClient();
		$ret = $ii_client->addMeta($source_id, $login_id, $title, $person, $tag, $miscellaneous);
	
		$this->assertEquals(TRUE, $ret);	
	
		$title = 'www' . time();
		$person = 'xx';
		$tag = 'yyy,zzz';
		$miscellaneous = array('tag2' => 'value2');
		$ret = $ii_client->addMeta($source_id, $login_id, $title, $person, $tag, $miscellaneous);
	
		$this->assertEquals(TRUE, $ret);
	}

	public function testUpdateMeta() {
		$source_id = ImageInstructureClientTest::$source_id;
		
		$login_id = 'qoo' . time();
		$title = '111';
		$person = '222';
		$tag = '333,444';
		$miscellaneous = array('555' => '666');
		
		$ii_client = new ImageInfrastructureClient();
		$ret = $ii_client->updateMeta($source_id, $login_id, $title, $person, $tag, $miscellaneous);
	
		$this->assertEquals(TRUE, $ret);
	}
	
	public function testUpdateAcl() {
		$login_id = 'qoo' . time();
		
		$source_id = ImageInstructureClientTest::$source_id;
		$destination_id = 1;
		$start_time = time();
		$end_time = $start_time + 86400;
		$country_allow_list = array('HK', 'CN');
		$country_deny_list = array('ALL');
		
		$ii_client = new ImageInfrastructureClient();
		$ret = $ii_client->updateAcl($source_id, $destination_id, $login_id, $start_time, $end_time, $country_allow_list, $country_deny_list);
	
		$this->assertEquals(TRUE, $ret);
	}
	
	public function testUpdateMetaManual() {
		$login_id = 'qoo' . time();
		
		$source_id = ImageInstructureClientTest::$source_id;
		$manual_tags = array('tag' => array('tag1', 'tag2'), 'person' => array('person1', 'person2'));
		
		$ii_client = new ImageInfrastructureClient();
		$ret = $ii_client->updateMetaManual($source_id, $login_id, $manual_tags);
	
		$this->assertEquals(TRUE, $ret);
	}
	
	public function testDeleteMetaManual() {
		$login_id = 'qoo' . time();
		
		$source_id = ImageInstructureClientTest::$source_id;
		
		$ii_client = new ImageInfrastructureClient();
		$ret = $ii_client->deleteMetaManual($source_id, $login_id);
	
		$this->assertEquals(TRUE, $ret);
	}

	public function testUpdateMetaArtiste() {
		$login_id = 'qoo' . time();
		
		$source_id = ImageInstructureClientTest::$source_id;
		$artiste_id = 1;
		$system_tags = array('artiste_tag1', 'artiste_tag2');
		
		$ii_client = new ImageInfrastructureClient();
		$ret = $ii_client->updateMetaArtiste($source_id, $artiste_id, $login_id, $system_tags);
	
		$this->assertEquals(TRUE, $ret);
	}
	
	public function testUpdateMetaBFarm() {
		$login_id = 'qoo' . time();
		
		$source_id = ImageInstructureClientTest::$source_id;
		$blog_id = 1;
		$system_tags = array('bfarm_tag1', 'bfarm_tag2');
		
		$ii_client = new ImageInfrastructureClient();
		$ret = $ii_client->updateMetaBFarm($source_id, $blog_id, $login_id, $system_tags);
	
		$this->assertEquals(TRUE, $ret);
	}
	
	public function testUpdateMetaBlog() {
		$login_id = 'qoo' . time();
		
		$source_id = ImageInstructureClientTest::$source_id;
		$blog_id = 1;
		$system_tags = array('blog_tag1', 'blog_tag2');
		
		$ii_client = new ImageInfrastructureClient();
		$ret = $ii_client->updateMetaBlog($source_id, $blog_id, $login_id, $system_tags);
	
		$this->assertEquals(TRUE, $ret);
	}
	
	public function testUpdateMetaCast() {
		$login_id = 'qoo' . time();
		
		$source_id = ImageInstructureClientTest::$source_id;
		$cast_id = 1;
		$system_tags = array('tag'=>'cast_tag1', 'title'=>'cast_tag2');
		
		$ii_client = new ImageInfrastructureClient();
		$ret = $ii_client->updateMetaCast($source_id, $cast_id, $login_id, $system_tags);
	
		$this->assertEquals(TRUE, $ret);
	}
		
	public function testUpdateMetaProgramme() {
		$login_id = 'qoo' . time();
		
		$source_id = ImageInstructureClientTest::$source_id;
		$programme_id = 1;
		$system_tags = array('programme_tag1', 'programme_tag2');
		
		$ii_client = new ImageInfrastructureClient();
		$ret = $ii_client->updateMetaProgramme($source_id, $programme_id, $login_id, $system_tags);
	
		$this->assertEquals(TRUE, $ret);
	}
	
	public function testUpdateMetaEpisode() {
		$login_id = 'qoo' . time();
		
		$source_id = ImageInstructureClientTest::$source_id;
		$episode_id = 1;
		$system_tags = array('episode_tag1', 'episode_tag2');
		
		$ii_client = new ImageInfrastructureClient();
		$ret = $ii_client->updateMetaEpisode($source_id, $episode_id, $login_id, $system_tags);
	
		$this->assertEquals(TRUE, $ret);
	}

	public function testRequestRecall() {
		$destination_id = 1;
		$login_id = 'qoo';
		$width = 50;
		$height = 50;
		$resize_mode = 2;
		$watermark = '';
		$format = '2';
		$export_arg = '85';
		
		$source_id = ImageInstructureClientTest::$source_id;
		
		$ii_client = new ImageInfrastructureClient();

		$export_id = $ii_client->requestRecall($login_id, $source_id, $width, $height, $resize_mode, $watermark, $format, $export_arg, $destination_id);
		
		$this->assertGreaterThanOrEqual(1, $export_id);
		
	}
	
	public function testDeleteSource() {
		$login_id = 'qoo' . time();
		
		$source_id = ImageInstructureClientTest::$source_id;
		
		$ii_client = new ImageInfrastructureClient();
		$ret = $ii_client->deleteSource($source_id);
	
		$this->assertEquals(TRUE, $ret);
		
		$url = $ii_client->getExportImageUrlEasily(ImageInstructureClientTest::$export_id);
		
		$this->assertEquals(-1, $url);
	}

	public function testRecoverSource() {
		$login_id = 'qoo' . time();
		
		$source_id = ImageInstructureClientTest::$source_id;
		
		$ii_client = new ImageInfrastructureClient();
		$ret = $ii_client->recoverSource($source_id);
	
		$this->assertEquals(TRUE, $ret);
		
		$url = $ii_client->getExportImageUrlEasily(ImageInstructureClientTest::$export_id);
		$this->assertRegExp('/http:\/\/ii\.tvb\.com\/.+/', $url);
	}

}
?>
